import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


public class EditRestaurantDialog extends JFrame {
	private JLabel titleLabel;
	private JList list;
	private JScrollPane listScroller;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private JLabel jLabel3;
	private JLabel jLabel4;
	private JLabel jLabel5;
	private JTextField zipCodeTextField;
	private JTextField restaurantNameField;
	private JTextField addressField;
	private JComboBox foodTypeList;
	private JButton editButton;
	private JCheckBox cheapButton;
	private JCheckBox affordableButton;
	private JCheckBox expensiveButton;
	private JCheckBox breakfastButton;
	private JCheckBox lunchButton;
	private JCheckBox brunchButton;
	private JCheckBox dinnerButton;
	private JCheckBox dessertButton;
	private JCheckBox romanticButton;
	private JCheckBox casualButton;
	private JCheckBox businessButton;
	private JCheckBox festiveButton;
	private JCheckBox quietButton;
	private JCheckBox twentyFourHrsButton;
	private JCheckBox deliveryButton;
	private JCheckBox sitDownButton;
	private JCheckBox driveThroughButton; 
	private JCheckBox takeOutButton;
	
	private ButtonGroup costGroup = new ButtonGroup();	
	private ButtonGroup mealGroup = new ButtonGroup();	
	private ButtonGroup typeGroup = new ButtonGroup();	
	private ButtonGroup moodGroup = new ButtonGroup();	
	
	JPanel restaurantNameRow = new JPanel();
    JPanel zipCodeRow = new JPanel();
	JPanel foodTypeRow = new JPanel();
	JPanel preferencesRow = new JPanel();
	JPanel okRow = new JPanel();
	
	public Constants cons;
	
	public EditRestaurantDialog() {
		
		Container pane = getContentPane();
		this.setSize(800,600);

		// Show list of restaurants
		list = new JList();
		listScroller = new JScrollPane(list);
		listScroller.setPreferredSize(new Dimension(250, 80));
		pane.add(listScroller, BorderLayout.LINE_START);	
		
		// Show edit panel
		JPanel editPanel = new JPanel();
		editPanel.setLayout(new GridLayout(0,1));
		
		//this.setPreferredSize(new Dimension(300,400));
		restaurantNameRow.setLayout(new BoxLayout(restaurantNameRow, BoxLayout.Y_AXIS));
		//zipCodeRow.setMaximumSize(new Dimension(100,75));

		zipCodeRow.setLayout(new BoxLayout(zipCodeRow, BoxLayout.Y_AXIS));
		//zipCodeRow.setMaximumSize(new Dimension(100,75));

		foodTypeRow.setLayout(new BoxLayout(foodTypeRow, BoxLayout.Y_AXIS));
		// foodTypeRow.setMaximumSize(new Dimension(100,75));

		preferencesRow.setLayout(new BoxLayout(preferencesRow, BoxLayout.X_AXIS));


		//add rows to main panel
		//this.add(zipCodeRow);
		//this.add(foodTypeRow);


		//------------------- name and address row elements ----------------------------
		jLabel4 = new JLabel("Restaurant Name:");
		restaurantNameRow.add(jLabel4);

		restaurantNameField = new JTextField();
		restaurantNameField.setMaximumSize(new Dimension(400,20));
		restaurantNameField.setEditable(false);
		restaurantNameRow.add(restaurantNameField);

		jLabel4 = new JLabel("Address:");
		restaurantNameRow.add(jLabel4);

		addressField = new JTextField();
		addressField.setMaximumSize(new Dimension(400,20));
		restaurantNameRow.add(addressField);

		editPanel.add(restaurantNameRow);

		//------------------- zip code row elements ----------------------------
		//jLabel1 = new JLabel("FindRestaurant");
		//zipCodeRow.add(jLabel1);

		// Enter Zip Code Text Box
		jLabel1 = new JLabel("Enter Zip Code:");
		zipCodeRow.add(jLabel1);

		zipCodeTextField = new JTextField();
		zipCodeTextField.setMaximumSize(new Dimension(400,20));
		zipCodeRow.add(zipCodeTextField);

		editPanel.add(zipCodeRow);
		// --------------- food type elts ------------------------------------
		jLabel2 = new JLabel("Choose a Type of Food:");
		foodTypeRow.add(jLabel2);
		String[] foodStrings = { "Burgers","Chinese", "Chicken", "Greek", "Italian", "Indian", "Mexican",
				"Pizza", "Steak"};
		foodTypeList = new JComboBox(foodStrings);
		foodTypeList.setSelectedIndex(4);
		foodTypeList.setMaximumSize(new Dimension(400,20));
		foodTypeRow.add(foodTypeList);

		jLabel3 = new JLabel("Select Preferences:");
		foodTypeRow.add(jLabel3);
		editPanel.add(foodTypeRow);
		// ----------------- preferences elts ----------------------------------

		//cost group buttons
		JPanel costGroupCol = new JPanel();
		costGroupCol.setLayout(new BoxLayout(costGroupCol, BoxLayout.Y_AXIS));
		cheapButton = new JCheckBox("Cheap (< $10)");
		affordableButton = new JCheckBox("Affordable ($10-$20)");	
		expensiveButton = new JCheckBox("Expensive (> $20)");
		//costGroup.add(cheapButton);
		//costGroup.add(affordableButton);
		//costGroup.add(expensiveButton);
		costGroupCol.add(cheapButton);
		costGroupCol.add(affordableButton);
		costGroupCol.add(expensiveButton);
		preferencesRow.add(costGroupCol);

		//meal group buttons
		JPanel mealGroupCol = new JPanel();
		mealGroupCol.setLayout(new BoxLayout(mealGroupCol, BoxLayout.Y_AXIS));
		breakfastButton = new JCheckBox("Breakfast");
		lunchButton = new JCheckBox("Lunch");
		brunchButton = new JCheckBox("Brunch");
		dinnerButton = new JCheckBox("Dinner");
		dessertButton = new JCheckBox("Desert");
		//mealGroup.add(lunchButton);
		//mealGroup.add(breakfastButton);
		//mealGroup.add(dinnerButton);
		//mealGroup.add(brunchButton);
		//mealGroup.add(desertButton);
		mealGroupCol.add(breakfastButton);
		mealGroupCol.add(lunchButton);
		mealGroupCol.add(brunchButton);
		mealGroupCol.add(dinnerButton);
		mealGroupCol.add(dessertButton);
		preferencesRow.add(mealGroupCol);


		//mood group buttons
		JPanel romanticGroupCol = new JPanel();
		romanticGroupCol.setLayout(new BoxLayout(romanticGroupCol, BoxLayout.Y_AXIS));
		romanticButton = new JCheckBox("Romantic");
		casualButton = new JCheckBox("Casual");
		businessButton = new JCheckBox("Business");
		festiveButton = new JCheckBox("Festive");
		quietButton = new JCheckBox("Quite");
		//moodGroup.add(romanticButton);
		//moodGroup.add(casualButton);
		//moodGroup.add(businessButton);
		//moodGroup.add(quiteButton);
		romanticGroupCol.add(romanticButton);
		romanticGroupCol.add(casualButton);
		romanticGroupCol.add(businessButton);
		romanticGroupCol.add(festiveButton);
		romanticGroupCol.add(quietButton);
		preferencesRow.add(romanticGroupCol);

		//type group buttons
		JPanel typeGroupCol = new JPanel();
		typeGroupCol.setLayout(new BoxLayout(typeGroupCol, BoxLayout.Y_AXIS));
		twentyFourHrsButton = new JCheckBox("24 hrs");
		deliveryButton = new JCheckBox("Delivery");
		sitDownButton = new JCheckBox("Sit Down");
		driveThroughButton = new JCheckBox("Drive Thru");
		takeOutButton = new JCheckBox("Take out");
		//typeGroup.add(twentyFourHrsButton);
		//typeGroup.add(sitDownButton);
		//typeGroup.add(driveThroughButton);
		//typeGroup.add(deliveryButton);
		typeGroupCol.add(twentyFourHrsButton);
		typeGroupCol.add(deliveryButton);
		typeGroupCol.add(sitDownButton);
		typeGroupCol.add(driveThroughButton);
		typeGroupCol.add(takeOutButton);
		preferencesRow.add(typeGroupCol);

		//preferencesRow.setPreferredSize(new Dimension(500, 500));
		//preferencesRow.setMinimumSize(new Dimension(800, 800));
		editPanel.add(preferencesRow);

		editButton = new JButton("edit");
		//searchButton.setMaximumSize(new Dimension (50,50));
		okRow.add(editButton);
		editPanel.add(okRow);
		
		pane.add(editPanel);
		



		/*
	public void itemStateChanged(ItemEvent e) {
	    ...
	    Object source = e.getItemSelectable();

	    if (source == chinButton) {
	        //...make a note of it...
	    } else if (source == glassesButton) {
	        //...make a note of it...
	    } else if (source == hairButton) {
	        //...make a note of it...
	    } else if (source == teethButton) {
	        //...make a note of it...
	    }


		jButton = new JButton("Sign in");
		this.add(jButton);
		 */
	}

	public JButton getEditButton(){
		return editButton;
	}
	
	public JList getList(){
		return list;
	}

	public String getRestaurantName(){
		return restaurantNameField.getText();
	}

	public String getAddressName(){
		return addressField.getText();
	}

	public String getZipCode(){
		return zipCodeTextField.getText();
	}

	public String getFoodType(){
		return (String)foodTypeList.getSelectedItem();
	}

	public Preferences getPreferences(){
		Preferences prefs = new Preferences();
		
		// check which preferences are chosen
		
		//------------cost------------
		if(cheapButton.isSelected()){
			prefs.addCost(Constants.CHEAP);
		}
		
		if(affordableButton.isSelected()){
			prefs.addCost(Constants.AFFORDABLE);
		}
		
		if(expensiveButton.isSelected()){
			prefs.addCost(Constants.EXPENSIVE);
		}
		
		
		//------------- meal ------------
		if(breakfastButton.isSelected()){
			prefs.addMeal(Constants.BREAKFAST);
		}
		
		if(lunchButton.isSelected()){
			prefs.addMeal(Constants.LUNCH);
		}
		
		if(dinnerButton.isSelected()){
			prefs.addMeal(Constants.DINNER);
		}
		
		if(brunchButton.isSelected()){
			prefs.addMeal(Constants.BRUNCH);
		}
		
		if(dessertButton.isSelected()){
			prefs.addMeal(Constants.DESSERT);
		}
		
		//-------------------- mood
		if(romanticButton.isSelected()){
			prefs.addMood(Constants.ROMANTIC);
		}
		
		if(businessButton.isSelected()){
			prefs.addMood(Constants.BUSINESS);
		}
		
		if(casualButton.isSelected()){
			prefs.addMood(Constants.CASUAL);
		}
		
		if(festiveButton.isSelected()){
			prefs.addMood(Constants.FESTIVE);
		}
		
		if(quietButton.isSelected()){
			prefs.addMood(Constants.QUIET);
		}
		
		//--------------- type
		if(twentyFourHrsButton.isSelected()){
			prefs.addType(Constants._24HRS);
		}
		
		if(driveThroughButton.isSelected()){
			prefs.addType(Constants.DRIVETHRU);
		}
		
		if(deliveryButton.isSelected()){
			prefs.addType(Constants.DELIVERY);
		}
		
		if(sitDownButton.isSelected()){
			prefs.addType(Constants.SITDOWN);
		}

		if(takeOutButton.isSelected()){
			prefs.addType(Constants.TAKEOUT);
		}

		return prefs;
		
		
		
	}

	public void update(Restaurant r) {
		restaurantNameField.setText(r.getName());
		addressField.setText(r.getAddress());
		zipCodeTextField.setText(r.getZipCode());
		foodTypeList.setSelectedItem(r.getFoodType());
		
		Preferences p = r.getPreferences();
		cheapButton.setSelected(p.getCost().contains(cons.CHEAP));
		affordableButton.setSelected(p.getCost().contains(cons.AFFORDABLE));
		expensiveButton.setSelected(p.getCost().contains(cons.EXPENSIVE));

		breakfastButton.setSelected(p.getMeal().contains(cons.BREAKFAST));
		lunchButton.setSelected(p.getMeal().contains(cons.LUNCH));
		brunchButton.setSelected(p.getMeal().contains(cons.BRUNCH));
		dinnerButton.setSelected(p.getMeal().contains(cons.DINNER));
		dessertButton.setSelected(p.getMeal().contains(cons.DESSERT));
		
		romanticButton.setSelected(p.getMood().contains(cons.ROMANTIC));
		casualButton.setSelected(p.getMood().contains(cons.CASUAL));
		businessButton.setSelected(p.getMood().contains(cons.BUSINESS));
		festiveButton.setSelected(p.getMood().contains(cons.FESTIVE));
		quietButton.setSelected(p.getMood().contains(cons.QUIET));
		
		twentyFourHrsButton.setSelected(p.getType().contains(cons._24HRS));
		deliveryButton.setSelected(p.getType().contains(cons.DELIVERY));
		sitDownButton.setSelected(p.getType().contains(cons.SITDOWN));
		driveThroughButton.setSelected(p.getType().contains(cons.DRIVETHRU));
		takeOutButton.setSelected(p.getType().contains(cons.TAKEOUT));
	}
	
	public void updateList(Vector<String> r) {
		list.setListData(r);
	}

}
